<!DOCTYPE HTML>
<html class="bg-eee">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>支付订单</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <link rel="stylesheet" type="text/css" href="../css/swiper.min.css" />
    <link rel="stylesheet" type="text/css" href="../css/layer.css" />
    <link rel="stylesheet" type="text/css" href="../css/mdw.css" />
    <script type="text/javascript" src="../script/api.js"></script>
    <script type="text/javascript" src="../script/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../script/swiper.min.js"></script>
    <script type="text/javascript" src="../script/mobile.js"></script>
    <script type="text/javascript" src="../script/layer.js"></script>
    <script type="text/javascript" src="../script/mdw.js"></script>
    <script type="text/javascript" src="../script/jumpPage.js"></script>

</head>
<body class="bg-eee">
    <header class="header">
        <div class="backPrevPage" onclick="BankIndexBtn()"></div>
        <div class="pageTitle">支付订单</div>
    </header>
    <section class="payPage HTML_BOX">
        <div class="payBox1 bg-fff">
            <div class="icon"></div>
            <div>
                <div class="price">$00.00</div>
                <div id="orderId">订单编号:<span></span></div>
            </div>
        </div>
        <div class="payBox2 plr">选择支付方式</div>
        <ul class="payList bg-fff">
            <li data-val="1">
                <div class="payItem">
                    <i class="icon"></i>
                    <span>信用卡</span>
                </div>
                <i class="radio"></i>
            </li>
            <li data-val="2">
                <div class="payItem">
                    <i class="icon"></i>
                    <span>Paypal/信用卡</span>
                </div>
                <i class="radio"></i>
            </li>
            <li data-val="3">
                <div class="payItem">
                    <i class="icon"></i>
                    <span>支付宝支付</span>
                </div>
                <i class="radio"></i>
            </li>
            <li data-val="4">
                <div class="payItem">
                    <i class="icon"></i>
                    <span>微信支付</span>
                </div>
                <i class="radio"></i>
            </li>
            <li data-val="5">
                <div class="payItem">
                    <i class="icon"></i>
                    <span>货到付款</span><span class="payTis">（刷卡/现金/EMT）</span>
                </div>
                <i class="radio"></i>
            </li>
        </ul>
        <div class="loginBtn f_c">前往支付</div>
    </section>
    <!-- 弹窗 -->
    <div class="mask_pop">
        <div class="mask" ></div>
        
        <div class="coupon_pop dn pop_Item">
            <div id="coupon_pop_title" style="margin-top: .5rem">即将跳转到PayPal支付</div>
            <div class="zuijiaorder" >完成支付后，返回APP再点击确认</div>
            <input type="hidden" id="zjorder_val" />
            <div style="display: flex;">
                <div class="zjorder_box1 f_c">确认支付</div>
                <div class="zjorder_box2 f_c">取消</div>
            </div>
        </div>
        
  </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
    
    $(".loginBtn").click(function(){
        pay();
    })

    $(".payList li").click(function(){
        $(".payList li").removeClass("on");
        $(this).addClass("on");
    })
    var param={};
    apiready = function(){
        $api.fixStatusBar($api.dom('header'));
        api.setStatusBarStyle({
            color: '#fff',//设置APP状态栏背景色
            style: 'dark'
        });
        BankIndex();
        ifCnAndEn();
        param = api.pageParam || {};
        console.log(JSON.stringify(param));
        $("#orderId span").html(param.order_number);
        $(".price").html('$'+param.sum_price);
        if(param.buy_type=="spell"){
           $(".payList li").eq(4).hide();
        }
        
    };
    function getJiaMoney(){
        getAjax(Interface.pay_total_money,{price:param.sum_price},function(res){
            console.log(JSON.stringify(res));
            param.sum_price = res.data;
        },function(res,err){})
    }
    window.addEventListener('popstate', function() {
        handler.close();
    });
    function pay(){
        var way = $(".payList li.on").data("val");
        var p = {
            token:token,
            pay_way:way,
            order_number:param.order_number,
            stripe_token:'sk_test_ti37oMwBAhAs6ZPFfBTvjBaJ'
        }
        if(way ==1){
            //stripePays();
            openWin('strpi','strpi.html',param);
            return;
        }
        pop.loading();
        getAjax(Interface.pay,p,function(res){
            pop.close();
            console.log(JSON.stringify(res));
            param.way = way;
            if(way ==1){
                stripePays();
            }else if(way ==2){
               
                // maskOpen();
                // $(".coupon_pop").removeClass("dn");
                // var s = 2;
                // if(cnen == 1){
                //         pop.notice('3秒后跳转PayPal',1);
                //     }else{
                //         pop.notice('Jump PayPal 3 seconds laterl',1);
                //     }
                // var djs = setInterval(function(){
                //     if(s == 0){
                //         clearInterval(djs);
                //     }
                //     if(cnen == 1){
                //         pop.notice(s+'秒后跳转PayPal',1);
                //     }else{
                //         pop.notice('Jump PayPal '+s+' seconds laterl',1)
                //     }
                //     s--;
                // },1000)
                // setTimeout(function(){
                //     openWeb(param.url);
                // },3000)
                paypal(res);
            }else if(way ==3){//支付宝
                alipay(res.data);
            }else if(way ==4){//微信
                wxpay(res.data);
            }else if(way ==5){
                if(param.buy_type=="spell"){
                    param.id= res.group_id;
                    openWin('groupBuy_details','groupBuy_details.html',param);
                }else{
                    openWin('ware_paySuccess','ware_paySuccess.html',param);
                }
            }
        },function(res,err){
            stripePays();
        })
    }

    //paypal
    function paypal(res){
          var strDM = api.systemType;
        if (strDM == 'ios') {
            var module = api.require('moduleDemo');
            module.showAlert({
                type:2,
                title:param.sum_price,
                msg:'买到网',
              },function(ret){
                pop.loading();
                getAjax(Interface.paypalb_notify,{order_number:param.order_number},function(res){
                    gopays();
                })
               
              });
        }else{
            console.log('安卓进入')
            var paypalModule = api.require('paypalmodule');
            paypalModule.startPay({
                title:0.01,
                msg:'买到网',
                appkey:'AX3KiyIwDk4e5bwznbJ6kPAOGrhEBGT8kyZdqqSjb5wmbApL1VI7dVza1Ayc8nTIQJFKKKo_3Gq7baGP',
                payType:'live',
                goods_brf:'Haveit.ca',
              },function(ret){
                //alert(JSON.stringify(ret));
                pop.loading();
                getAjax(Interface.paypalb_notify,{order_number:param.order_number},function(res){
                    gopays();
                })
              });
            
        }

        
        
        
        // var paypal = api.require('paypal');
        //     paypal.pay({
        //         currency: 'CAD',
        //         price: res.price,
        //         description: 'Haveit Ware',
        //         mode: 'noNetwork'
        //     }, function(ret) {
        //         console.log(JSON.stringify(ret));
        //         if (ret) {
        //            gopays();
        //         }
        //     });
    }

    function stripePays(){
            var strDM = api.systemType;
            if (strDM == 'ios') {
                console.log('sestr');
                var module = api.require('moduleDemo');
                    module.showAlert({
                    type:1,
                    title:param.sum_price,
                    msg:'买到网',
                  },function(ret){
                    // pop.loading();
                    // getAjax(Interface.paypalb_notify,{order_number:param.order_number},function(res){
                    //     gopays();
                    // })
                   
                  });
            } 
        }
    $(".zjorder_box1").click(function(){
        gopays();
    })
    //取消追加订单
    $(".zjorder_box2").click(function(){
        maskClose();
        $(".pop_Item").addClass("dn");
    })
    //支付宝
     function alipay(orderno){
        var aliPayPlus = api.require('aliPayPlus');
            aliPayPlus.payOrder({
                orderInfo:orderno
            }, function(ret, err) {
                if (ret.code == 9000) {
                        pop.notice('支付成功');
                        getJiaMoney();
                        param.sum_price = param.sum_price / 100;
                        gopays();
                    } else {
                        if (ret.code == 6001) {
                            pop.notice('支付已取消');
                        } else {
                            pop.notice('支付失败');
                            console.log('支付失败 code:' + err.code + err.msg);
                        }
                    }
            });
    }
    //微信
    function wxpay(rem){
        var apiKey = 'wxff4f639d47c1a958';
        var mchId = '1497376172';
        var partnerKey = '9b7aee20f5a6f68365dbf0f68611d681';
        var notifyUrl = 'http://www.maplemellow.com/App/Pay/wx_notify';
        var wxPay = api.require('wxPay');
        wxPay.config({
            apiKey: apiKey,
            mchId: mchId,
            partnerKey: partnerKey,
            notifyUrl: notifyUrl
        }, function(ret, err) {
            console.log(JSON.stringify(ret));
            if (ret.status) {
                //alert('配置商户支付参数成功');
                getJiaMoney();
                console.log(param.sum_price);
                wxPay.pay({
                    description:'买到网',
                    totalFee: param.sum_price,
                    tradeNo:"wx"+param.order_number,
                },function(ret,err){
                    console.log(JSON.stringify(ret));
                    console.log(JSON.stringify(err));
                    if(ret.status) {
                        pop.notice('支付成功');
                        param.sum_price = param.sum_price / 100;
                        gopays();
                    } else {
                        if(err.code == '-2'){
                             pop.notice('取消支付');
                        } else {
                            pop.notice('支付失败');
                        }
                    }
                });
            } else {
                alert(err.code);
            }
        });
    }
    function gopays(){
        var p = {order_number:param.order_number};
        pop.loading();
        getAjax(Interface.pay_ok,p,function(res){
            pop.close();
            console.log(JSON.stringify(res));
            console.log(param.sum_price);
            if(param.buy_type=="spell"){
                param.id= res.group_id;
                openWin('groupBuy_details','groupBuy_details.html',param);
            }else{
                openWin('ware_paySuccess','ware_paySuccess.html',param);
            }
        },function(res,err){

        })
    }
</script>
<script type="text/javascript">
    function ifCnAndEn(){
        if(cnen==2){
            $("#orderId").html('Oder No:<span></span>');
            $(".pageTitle").eq(0).html('Select payment methods');
            $(".payBox2").html('Select a method');
            $(".payList li").eq(1).find('.payItem span').html('Paypal/Credit Card');
              $(".payList li").eq(0).find('.payItem span').html('Credit Card');
            $(".payList li").eq(2).find('.payItem span').html('Alipay Pay');
            $(".payList li").eq(3).find('.payItem span').html('WeChat Pay');
            $(".payList li").eq(4).find('.payItem span').eq(0).html('Collect on Delivery');
            $(".payList li").eq(4).find('.payItem span').eq(1).html('(Card/Cash/EMT)');
            $(".loginBtn").html("Pay Now");
            $("#coupon_pop_title").html('Be about to jump to PayPal Payment?');
            $(".zuijiaorder").html('After payment is completed, return to APP and click confirmation again.');
            $(".zjorder_box1").html('Success');
            $(".zjorder_box2").html('Cancel');

        }
    }
</script>
</html>